package com.xumx.demo.customize.exception;

import com.xumx.demo.pojo.ResponseObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice  //统一处理
public class GlobalExceptionHandlerAdvice {

    Logger logger = Logger.getLogger(GlobalExceptionHandlerAdvice.class);

    @ExceptionHandler({Exception.class}) //什么异常的统一处理
    public ResponseObject handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
        //记录日志
        logger.error(e.getMessage(), e);
        return new ResponseObject(500, "error", e.getMessage());
    }
}
